﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;

namespace He.Web.UI
{
    /// <summary>
    /// 网站的页面基类
    /// </summary>
    public partial class PageBase : System.Web.UI.Page
    {

        public PagingInfo PagingInfo = new PagingInfo();

        /// <summary>
        /// 获取语言栏目信息
        /// </summary>
        /// <param name="lang"></param>
        /// <returns></returns>
        public Model.site_info GetSiteInfo(string lang)
        {
            return new BLL.site_info().GetModelByLang(lang);
        }

        /// <summary>
        /// 获取频道标题
        /// </summary>
        /// <param name="channel_id">频道ID</param>
        /// <returns></returns>
        public string GetChannelTitle(object channel_id)
        {
            He.BLL.channel bll = new BLL.channel();
            He.Model.channel model = bll.GetModelByCache(Convert.ToInt32(channel_id));
            return model.title;
        }
        /// <summary>
        /// 取得分类标题
        /// </summary>
        /// <param name="category_id">类别ID</param>
        /// <returns></returns>
        public string GetCategorylTitle(object _category_id)
        {
            Model.category model = new BLL.category().GetModel(Convert.ToInt32(_category_id));
            if (model == null)
            {
                return "无";
            }
            else
            {
                return model.title;
            }
        }

        /// <summary>
        /// 取得分类标题
        /// </summary>
        /// <param name="_category_id">类别ID</param>
        /// <param name="_defaultValue">默认值</param>
        /// <returns></returns>
        public string GetCategorylTitle(object _category_id, string _defaultValue)
        {
            He.Model.category model = new He.BLL.category().GetModel(Convert.ToInt32(_category_id));
            if (model == null)
            {
                return _defaultValue;
            }
            else
            {
                return model.title;
            }
        }

        /// <summary>
        /// 取得评论总数
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        public int GetCommentCount(int cid, int pid)
        {
            return new BLL.comment().GetRecordCount("channel_id=" + cid + " and pid=" + pid);
        }

        /// <summary>
        /// 根据键名获取请求(Request)的相应参数值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="underline">转换失败时返回的值</param>
        /// <returns></returns>
        public int GetInt(string key, int underline)
        {
            string text = Request.Params[key] as string;
            return ToInt(text, underline);
        }

        /// <summary>
        /// 将字符串转化为整型
        /// </summary>
        /// <param name="text"></param>
        /// <param name="underline">转换失败时返回的值</param>
        /// <returns></returns>
        public int ToInt(string text, int underline)
        {
            int result;

            bool flag = int.TryParse(text, out result);

            return flag ? result : underline;
        }


        public string GetString(string key)
        {
            string rtn = string.Empty;

            if (!string.IsNullOrEmpty(Request.QueryString[key]))
            {
                rtn = Request.QueryString[key].Trim();

                //过滤危险字符
                rtn = Request.QueryString[key].Trim();

                rtn = ImmitPrevent(rtn);
            }

            return rtn;
        }

        /// <summary>
        /// 防注入
        /// 日期：2007-6-28
        /// 作者：RoseKing
        /// </summary>
        /// <param name="getStr">要处理的字符串</param>
        /// <returns>返回处理好的字符串</returns>
        public string ImmitPrevent(string getStr)
        {
            string Str;
            Str = getStr.Replace("\"\"", "&quot;");
            Str = Str.Replace("<", "&lt;");
            Str = Str.Replace(">", "&gt;");
            Str = Str.Replace("script", "&#115;cript");
            Str = Str.Replace("SCRIPT", "&#083;CRIPT");
            Str = Str.Replace("Script", "&#083;cript");
            Str = Str.Replace("script", "&#083;cript");
            Str = Str.Replace("object", "&#111;bject");
            Str = Str.Replace("OBJECT", "&#079;BJECT");
            Str = Str.Replace("Object", "&#079;bject");
            Str = Str.Replace("object", "&#079;bject");
            Str = Str.Replace("applet", "&#097;pplet");
            Str = Str.Replace("APPLET", "&#065;PPLET");
            Str = Str.Replace("Applet", "&#065;pplet");
            Str = Str.Replace("applet", "&#065;pplet");
            Str = Str.Replace("[", "&#091;");
            Str = Str.Replace("]", "&#093;");
            Str = Str.Replace("\"\"", "");
            Str = Str.Replace("=", "&#061;");
            Str = Str.Replace("'", "");
            Str = Str.Replace("select", "sel&#101;ct");
            Str = Str.Replace("execute", "&#101xecute");
            Str = Str.Replace("exec", "&#101xec");
            Str = Str.Replace("join", "jo&#105;n");
            Str = Str.Replace("union", "un&#105;on");
            Str = Str.Replace("where", "wh&#101;re");
            Str = Str.Replace("insert", "ins&#101;rt");
            Str = Str.Replace("delete", "del&#101;te");
            Str = Str.Replace("update", "up&#100;ate");
            Str = Str.Replace("like", "lik&#101;");
            Str = Str.Replace("drop", "dro&#112;");
            Str = Str.Replace("create", "cr&#101;ate");
            Str = Str.Replace("rename", "ren&#097;me");
            Str = Str.Replace("count", "co&#117;nt");
            Str = Str.Replace("chr", "c&#104;r");
            Str = Str.Replace("mid", "m&#105;d");
            Str = Str.Replace("truncate", "trunc&#097;te");
            Str = Str.Replace("nchar", "nch&#097;r");
            Str = Str.Replace("char", "ch&#097;r");
            Str = Str.Replace("alter", "alt&#101;r");
            Str = Str.Replace("cast", "ca&#115;t");
            Str = Str.Replace("exists", "e&#120;ists");
            return Str;
        }



        #region 函数库

        public string ToHtml(string input)
        {
            StringBuilder sb = new StringBuilder(input);
            //sb.Replace("&", "&amp;");
            //sb.Replace("<", "&lt;");
            //sb.Replace(">", "&gt;");
            sb.Replace("\r", "&nbsp;&nbsp;");
            sb.Replace(" ", "&nbsp;");
            sb.Replace("\n", "<br />");
            return sb.ToString();
        }

        public string ToTxt(string input)
        {
            StringBuilder sb = new StringBuilder(input);
            sb.Replace("&nbsp;", " ");
            sb.Replace("<br>", "\r\n");
            sb.Replace("<br>", "\n");
            sb.Replace("<br />", "\n");
            sb.Replace("<br />", "\r\n");
            sb.Replace("&lt;", "<");
            sb.Replace("&gt;", ">");
            sb.Replace("&amp;", "&");
            return sb.ToString();
        }

        public static string SubString(string src, int limit)
        {
            if (src.Length > limit)
            {
                return src.Substring(0, limit) + "…";
            }
            return src;
        }

        public string SubString(object o, int limit)
        {
            return SubString(o.ToString(), limit);
        }


        /// <summary>
        /// 根据地址长度自动换行
        /// </summary>
        public string subAddress(object o, int indexNum)
        {
            string address = "";
            if (o.ToString().Length > indexNum + 1)
            {
                address = o.ToString().Substring(0, indexNum);
                address += "<br />" + o.ToString().Substring(indexNum);
            }
            else
            {
                address = o.ToString();
            }
            return address;
        }
        #endregion

    }
}
